﻿///<reference path="FileInfoModel.ts"/>
///<reference path="ProductVariantModel.ts"/>
///<reference path="ParametricModel.ts"/>

module Enferno.Public.Web.Components.Models {
    
    export class ProductModel {
        public id: number;
        public description: string;
        public descriptionHeader: string;
        public flags: Array<number>;
        public name: string;
        public unitOfMeasurement: string;
        public onHandStatusText1: string;
        public onHandStatusText2: string;
        public onHandStatusCount: string;
        public displayPrice: number;
        public catalogPrice: number;
        public vatRate: number;
        public priceListId: number;
        public recommendedQuantity: number;
        public statusId: number;
        public subHeader: string;
        public uniqueName: string;
        public files: Array<FileInfoModel>;

        public variants: Array<ProductVariantModel>;
        public variantParametrics: Array<ParametricModel>;

        constructor();
        constructor(serverObject: any);
        constructor(serverObject?: any) {
            this.files = new Array<FileInfoModel>();
            this.variants = new Array<ProductVariantModel>();
            this.variantParametrics = new Array<ParametricModel>();

            if (serverObject != null) {
                this.createFromServerObject(serverObject);
            }
        }

        private createFromServerObject(serverObject: any) {
            this.id = serverObject.id;
            this.description = serverObject.description;
            this.descriptionHeader = serverObject.descriptionHeader;
            this.flags = serverObject.flags;
            this.name = serverObject.name;
            this.unitOfMeasurement = serverObject.unitOfMeasurement;
            this.onHandStatusText1 = serverObject.onHandStatusText1;
            this.onHandStatusText2 = serverObject.onHandStatusText2;
            this.onHandStatusCount = serverObject.onHandStatusCount;
            this.recommendedQuantity = serverObject.recommendedQuantity;
            this.displayPrice = serverObject.displayPrice;
            this.catalogPrice = serverObject.catalogPrice;
            this.vatRate = serverObject.vatRate;
            this.priceListId = serverObject.priceListId;

            Enumerable.from(serverObject.variants).forEach(variant => {
                this.variants.push(new ProductVariantModel(variant));
            });

            Enumerable.from(serverObject.variantParametrics).forEach(variantParametric => {
                this.variantParametrics.push(new ParametricModel(variantParametric));
            });

            this.files = new Array<FileInfoModel>();
            Enumerable.from(serverObject.files).forEach(fileInfo => {
                this.files.push(new FileInfoModel(fileInfo));
            });
        }
    }

} 